.class public Lcom/melesta/engine/DbBackup;
.super Ljava/lang/Object;
.source "DbBackup.java"


# static fields
.field protected static final SAVE_FILES_ON_DISK:Z


# direct methods
.method public constructor <init>()V
    .locals 0

    .prologue
    .line 16
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method

.method static backupAll(Lcom/melesta/engine/DatabaseHelper;)[B
    .locals 6
    .parameter "dbh"

    .prologue
    .line 127
    new-instance v1, Ljava/io/ByteArrayOutputStream;

    invoke-direct {v1}, Ljava/io/ByteArrayOutputStream;-><init>()V

    .line 128
    .local v1, bufStream:Ljava/io/ByteArrayOutputStream;
    new-instance v3, Ljava/io/DataOutputStream;

    invoke-direct {v3, v1}, Ljava/io/DataOutputStream;-><init>(Ljava/io/OutputStream;)V

    .line 130
    .local v3, out:Ljava/io/DataOutputStream;
    :try_start_0
    invoke-static {p0}, Lcom/melesta/engine/DbBackup;->backupFiles(Lcom/melesta/engine/DatabaseHelper;)[B

    move-result-object v0

    .line 131
    .local v0, buf:[B
    invoke-static {v3, v0}, Lcom/melesta/engine/DbBackup;->storeBytes(Ljava/io/DataOutputStream;[B)V

    .line 133
    invoke-static {p0}, Lcom/melesta/engine/DbBackup;->backupTransactions(Lcom/melesta/engine/DatabaseHelper;)[B

    move-result-object v0

    .line 134
    invoke-static {v3, v0}, Lcom/melesta/engine/DbBackup;->storeBytes(Ljava/io/DataOutputStream;[B)V

    .line 136
    invoke-virtual {v1}, Ljava/io/ByteArrayOutputStream;->toByteArray()[B
    :try_end_0
    .catchall {:try_start_0 .. :try_end_0} :catchall_0
    .catch Ljava/io/IOException; {:try_start_0 .. :try_end_0} :catch_0

    move-result-object v4

    .line 142
    :try_start_1
    invoke-virtual {v3}, Ljava/io/DataOutputStream;->close()V

    .line 143
    invoke-virtual {v1}, Ljava/io/ByteArrayOutputStream;->close()V
    :try_end_1
    .catch Ljava/lang/Exception; {:try_start_1 .. :try_end_1} :catch_3

    .line 145
    .end local v0           #buf:[B
    :goto_0
    return-object v4

    .line 137
    :catch_0
    move-exception v2

    .line 138
    .local v2, e:Ljava/io/IOException;
    :try_start_2
    invoke-static {v2}, Lcom/melesta/engine/Error;->processException(Ljava/lang/Exception;)V
    :try_end_2
    .catchall {:try_start_2 .. :try_end_2} :catchall_0

    .line 139
    const/4 v4, 0x0

    .line 142
    :try_start_3
    invoke-virtual {v3}, Ljava/io/DataOutputStream;->close()V

    .line 143
    invoke-virtual {v1}, Ljava/io/ByteArrayOutputStream;->close()V
    :try_end_3
    .catch Ljava/lang/Exception; {:try_start_3 .. :try_end_3} :catch_1

    goto :goto_0

    .line 144
    :catch_1
    move-exception v5

    goto :goto_0

    .line 141
    .end local v2           #e:Ljava/io/IOException;
    :catchall_0
    move-exception v4

    .line 142
    :try_start_4
    invoke-virtual {v3}, Ljava/io/DataOutputStream;->close()V

    .line 143
    invoke-virtual {v1}, Ljava/io/ByteArrayOutputStream;->close()V
    :try_end_4
    .catch Ljava/lang/Exception; {:try_start_4 .. :try_end_4} :catch_2

    .line 145
    :goto_1
    throw v4

    .line 144
    :catch_2
    move-exception v5

    goto :goto_1

    .restart local v0       #buf:[B
    :catch_3
    move-exception v5

    goto :goto_0
.end method

.method static backupFiles(Lcom/melesta/engine/DatabaseHelper;)[B
    .locals 4
    .parameter "dbh"

    .prologue
    .line 21
    new-instance v0, Ljava/io/ByteArrayOutputStream;

    invoke-direct {v0}, Ljava/io/ByteArrayOutputStream;-><init>()V

    .line 22
    .local v0, bufStream:Ljava/io/ByteArrayOutputStream;
    new-instance v2, Ljava/io/DataOutputStream;

    invoke-direct {v2, v0}, Ljava/io/DataOutputStream;-><init>(Ljava/io/OutputStream;)V

    .line 24
    .local v2, outWriter:Ljava/io/DataOutputStream;
    new-instance v3, Lcom/melesta/engine/DbBackup$1;

    invoke-direct {v3, v2}, Lcom/melesta/engine/DbBackup$1;-><init>(Ljava/io/DataOutputStream;)V

    invoke-virtual {p0, v3}, Lcom/melesta/engine/DatabaseHelper;->forEachFile(Lcom/melesta/engine/DatabaseHelper$IDbFileProcessor;)V

    .line 45
    invoke-virtual {v0}, Ljava/io/ByteArrayOutputStream;->toByteArray()[B

    move-result-object v1

    .line 46
    .local v1, buffer:[B
    return-object v1
.end method

.method static backupTransactions(Lcom/melesta/engine/DatabaseHelper;)[B
    .locals 8
    .parameter "dbh"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 50
    new-instance v0, Ljava/io/ByteArrayOutputStream;

    invoke-direct {v0}, Ljava/io/ByteArrayOutputStream;-><init>()V

    .line 51
    .local v0, bufStream:Ljava/io/ByteArrayOutputStream;
    new-instance v3, Ljava/io/DataOutputStream;

    invoke-direct {v3, v0}, Ljava/io/DataOutputStream;-><init>(Ljava/io/OutputStream;)V

    .line 53
    .local v3, outWriter:Ljava/io/DataOutputStream;
    invoke-virtual {p0}, Lcom/melesta/engine/DatabaseHelper;->getTransactions()Ljava/util/List;

    move-result-object v6

    .line 55
    .local v6, transactions:Ljava/util/List;,"Ljava/util/List<Lcom/melesta/payment/PaymentTransaction;>;"
    invoke-interface {v6}, Ljava/util/List;->iterator()Ljava/util/Iterator;

    move-result-object v2

    .local v2, i$:Ljava/util/Iterator;
    :goto_0
    invoke-interface {v2}, Ljava/util/Iterator;->hasNext()Z

    move-result v7

    if-eqz v7, :cond_0

    invoke-interface {v2}, Ljava/util/Iterator;->next()Ljava/lang/Object;

    move-result-object v5

    check-cast v5, Lcom/melesta/payment/PaymentTransaction;

    .line 56
    .local v5, tr:Lcom/melesta/payment/PaymentTransaction;
    invoke-virtual {v5}, Lcom/melesta/payment/PaymentTransaction;->serialize()[B

    move-result-object v4

    .line 57
    .local v4, serializedTr:[B
    array-length v7, v4

    invoke-virtual {v3, v7}, Ljava/io/DataOutputStream;->writeInt(I)V

    .line 58
    invoke-virtual {v3, v4}, Ljava/io/DataOutputStream;->write([B)V

    goto :goto_0

    .line 61
    .end local v4           #serializedTr:[B
    .end local v5           #tr:Lcom/melesta/payment/PaymentTransaction;
    :cond_0
    invoke-virtual {v0}, Ljava/io/ByteArrayOutputStream;->toByteArray()[B

    move-result-object v1

    .line 62
    .local v1, buffer:[B
    return-object v1
.end method

.method public static restoreAll(Lcom/melesta/engine/DatabaseHelper;[B)Z
    .locals 8
    .parameter "dbh"
    .parameter "data"

    .prologue
    const/4 v5, 0x0

    .line 150
    array-length v6, p1

    const/16 v7, 0x9

    if-ge v6, v7, :cond_0

    .line 170
    :goto_0
    return v5

    .line 154
    :cond_0
    :try_start_0
    new-instance v0, Ljava/io/ByteArrayInputStream;

    invoke-direct {v0, p1}, Ljava/io/ByteArrayInputStream;-><init>([B)V

    .line 155
    .local v0, baStream:Ljava/io/ByteArrayInputStream;
    new-instance v3, Ljava/io/DataInputStream;

    invoke-direct {v3, v0}, Ljava/io/DataInputStream;-><init>(Ljava/io/InputStream;)V

    .line 156
    .local v3, in:Ljava/io/DataInputStream;
    invoke-virtual {v3}, Ljava/io/DataInputStream;->readInt()I

    move-result v4

    .line 157
    .local v4, sz:I
    new-array v1, v4, [B

    .line 158
    .local v1, buf:[B
    invoke-virtual {v3, v1}, Ljava/io/DataInputStream;->read([B)I

    .line 159
    invoke-static {p0, v1}, Lcom/melesta/engine/DbBackup;->restoreFiles(Lcom/melesta/engine/DatabaseHelper;[B)V

    .line 161
    invoke-virtual {v3}, Ljava/io/DataInputStream;->readInt()I

    move-result v4

    .line 162
    new-array v1, v4, [B

    .line 163
    invoke-virtual {v3, v1}, Ljava/io/DataInputStream;->read([B)I

    .line 164
    invoke-static {p0, v1}, Lcom/melesta/engine/DbBackup;->restoreTransactions(Lcom/melesta/engine/DatabaseHelper;[B)V
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    .line 170
    const/4 v5, 0x1

    goto :goto_0

    .line 165
    .end local v0           #baStream:Ljava/io/ByteArrayInputStream;
    .end local v1           #buf:[B
    .end local v3           #in:Ljava/io/DataInputStream;
    .end local v4           #sz:I
    :catch_0
    move-exception v2

    .line 166
    .local v2, e:Ljava/lang/Exception;
    invoke-static {v2}, Lcom/melesta/engine/Error;->processException(Ljava/lang/Exception;)V

    goto :goto_0
.end method

.method static restoreFiles(Lcom/melesta/engine/DatabaseHelper;Ljava/io/DataInputStream;)V
    .locals 9
    .parameter "dbh"
    .parameter "in"

    .prologue
    .line 66
    invoke-virtual {p0}, Lcom/melesta/engine/DatabaseHelper;->deleteAllFiles()V

    .line 70
    :goto_0
    :try_start_0
    invoke-virtual {p1}, Ljava/io/DataInputStream;->readUTF()Ljava/lang/String;

    move-result-object v2

    .line 71
    .local v2, fileName:Ljava/lang/String;
    invoke-virtual {p1}, Ljava/io/DataInputStream;->readInt()I

    move-result v3

    .line 72
    .local v3, fileSize:I
    new-array v0, v3, [B

    .line 73
    .local v0, buf:[B
    invoke-virtual {p1, v0}, Ljava/io/DataInputStream;->read([B)I

    .line 75
    const-string v4, "engine"

    const-string v5, "Backup-restore(\'%s\', %d)"

    const/4 v6, 0x2

    new-array v6, v6, [Ljava/lang/Object;

    const/4 v7, 0x0

    aput-object v2, v6, v7

    const/4 v7, 0x1

    invoke-static {v3}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer;

    move-result-object v8

    aput-object v8, v6, v7

    invoke-static {v5, v6}, Ljava/lang/String;->format(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;

    move-result-object v5

    invoke-static {v4, v5}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

    .line 90
    invoke-virtual {p0, v2, v0}, Lcom/melesta/engine/DatabaseHelper;->saveFile(Ljava/lang/String;[B)V
    :try_end_0
    .catch Ljava/io/IOException; {:try_start_0 .. :try_end_0} :catch_0

    goto :goto_0

    .line 91
    .end local v0           #buf:[B
    .end local v2           #fileName:Ljava/lang/String;
    .end local v3           #fileSize:I
    :catch_0
    move-exception v1

    .line 95
    .local v1, ex:Ljava/io/IOException;
    return-void
.end method

.method static restoreFiles(Lcom/melesta/engine/DatabaseHelper;[B)V
    .locals 2
    .parameter "dbh"
    .parameter "dataBuf"

    .prologue
    .line 98
    new-instance v0, Ljava/io/ByteArrayInputStream;

    invoke-direct {v0, p1}, Ljava/io/ByteArrayInputStream;-><init>([B)V

    .line 99
    .local v0, baStream:Ljava/io/ByteArrayInputStream;
    new-instance v1, Ljava/io/DataInputStream;

    invoke-direct {v1, v0}, Ljava/io/DataInputStream;-><init>(Ljava/io/InputStream;)V

    .line 101
    .local v1, in:Ljava/io/DataInputStream;
    invoke-static {p0, v1}, Lcom/melesta/engine/DbBackup;->restoreFiles(Lcom/melesta/engine/DatabaseHelper;Ljava/io/DataInputStream;)V

    .line 102
    return-void
.end method

.method static restoreTransactions(Lcom/melesta/engine/DatabaseHelper;Ljava/io/DataInputStream;)V
    .locals 4
    .parameter "dbh"
    .parameter "in"

    .prologue
    .line 107
    :goto_0
    :try_start_0
    invoke-virtual {p1}, Ljava/io/DataInputStream;->readInt()I

    move-result v3

    .line 108
    .local v3, trSize:I
    new-array v0, v3, [B

    .line 109
    .local v0, buf:[B
    invoke-virtual {p1, v0}, Ljava/io/DataInputStream;->read([B)I

    .line 111
    invoke-static {v0}, Lcom/melesta/payment/PaymentTransaction;->deserialize([B)Lcom/melesta/payment/PaymentTransaction;

    move-result-object v2

    .line 113
    .local v2, tr:Lcom/melesta/payment/PaymentTransaction;
    invoke-virtual {p0, v2}, Lcom/melesta/engine/DatabaseHelper;->saveTransaction(Lcom/melesta/payment/PaymentTransaction;)J
    :try_end_0
    .catch Ljava/io/IOException; {:try_start_0 .. :try_end_0} :catch_0

    goto :goto_0

    .line 114
    .end local v0           #buf:[B
    .end local v2           #tr:Lcom/melesta/payment/PaymentTransaction;
    .end local v3           #trSize:I
    :catch_0
    move-exception v1

    .line 118
    .local v1, ex:Ljava/io/IOException;
    return-void
.end method

.method static restoreTransactions(Lcom/melesta/engine/DatabaseHelper;[B)V
    .locals 2
    .parameter "dbh"
    .parameter "dataBuf"

    .prologue
    .line 121
    new-instance v0, Ljava/io/ByteArrayInputStream;

    invoke-direct {v0, p1}, Ljava/io/ByteArrayInputStream;-><init>([B)V

    .line 122
    .local v0, baStream:Ljava/io/ByteArrayInputStream;
    new-instance v1, Ljava/io/DataInputStream;

    invoke-direct {v1, v0}, Ljava/io/DataInputStream;-><init>(Ljava/io/InputStream;)V

    .line 123
    .local v1, in:Ljava/io/DataInputStream;
    invoke-static {p0, v1}, Lcom/melesta/engine/DbBackup;->restoreTransactions(Lcom/melesta/engine/DatabaseHelper;Ljava/io/DataInputStream;)V

    .line 124
    return-void
.end method

.method private static storeBytes(Ljava/io/DataOutputStream;[B)V
    .locals 1
    .parameter "out"
    .parameter "buf"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 174
    array-length v0, p1

    invoke-virtual {p0, v0}, Ljava/io/DataOutputStream;->writeInt(I)V

    .line 175
    array-length v0, p1

    if-lez v0, :cond_0

    .line 176
    invoke-virtual {p0, p1}, Ljava/io/DataOutputStream;->write([B)V

    .line 178
    :cond_0
    return-void
.end method
